#include "entity.h"
extern QString globalFHDirPath;
QStringList getFlyFireParticleHeaders() {
    QStringList headers;
    headers << QObject::tr("飞火颗粒编号")
            << QObject::tr("飞火颗粒材料名称")
            << QObject::tr("飞火颗粒材料种类")
            << QObject::tr("燃烧前飞火颗粒质量(g)")
            << QObject::tr("燃烧前飞火颗粒尺寸(mm)")
            << QObject::tr("飞火颗粒加工方法")
            << QObject::tr("燃烧后飞火颗粒质量(g)")
            << QObject::tr("燃烧后飞火颗粒尺寸(mm)")
            << QObject::tr("飞火颗粒数目（个）")
            << QObject::tr("飞火颗粒来源或产地")
            << QObject::tr("飞火颗粒图片");
    return headers;
}

FlyFireParticle importCSV(const QString &filePath) {
    if (filePath.isEmpty())
    {
        qDebug() << "当前打开的文件路径为" << filePath << "打开失败！";
        // 返回一个默认构造的 FlyFireParticle 对象
        return FlyFireParticle();
    }else {
        //显示文件路径
        qDebug() << "当前打开的文件路径为" << filePath;
    }

    // 初始化飞火颗粒结构体
    FlyFireParticle particle;

    QFileInfo fileInfo(filePath);
    QString baseName = fileInfo.completeBaseName(); // 获取不包含扩展名的文件名
    qDebug() << "去除扩展名后的文件名为：" << baseName;

    // 读取文件内容
    QFile file(filePath);
    if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
        qDebug() << "无法打开文件：" << filePath;
    }
    QTextStream in(&file);
    // 指定文件编码为GB2312
    in.setCodec(QTextCodec::codecForName("GB2312"));
    QString content;

    QString directoryPath = fileInfo.dir().absolutePath(); // 获取包含文件名的完整路径

    qDebug() << "测试路径为：" << directoryPath;

    // 去掉文件名，保留目录路径
    directoryPath = directoryPath.left(directoryPath.lastIndexOf("/"));
    // 确保目录路径以正斜杠结束
    if (!directoryPath.endsWith("/")) {
        directoryPath += "/";
    }

    qDebug() << "保留路径为：" << directoryPath;

    while (!in.atEnd()) {
        QString line = in.readLine();
        // 这里可以对每一行数据进行处理
        content.append(line);
        //qDebug() << line << "\n";

        QByteArray byteArray = line.toUtf8();
        QList<QByteArray> fields = byteArray.split(',');
        int column = fields[0].toInt() - 1;
        QString itemText;
        switch (column) {
        case 0: // 飞火颗粒编号
            // 在读取CSV时已经设置了正确的编码，因此不需要再次转换
            // QString itemText = QString::fromLatin1(fields[2]);
            particle.particle_id = fields[2];
            itemText = particle.particle_id;
            break;
        case 1: // 飞火颗粒材料名称
            particle.material_name = fields[2];
            itemText = particle.material_name;
            break;
        case 2: // 飞火颗粒材料种类
            particle.material_type = fields[2];
            itemText = particle.material_type;
            break;
        case 3: // 燃烧前飞火颗粒质量(g)
            particle.pre_burn_weight = fields[2].toDouble();
            itemText = QString::number(particle.pre_burn_weight);
            break;
        case 4: // 燃烧前飞火颗粒尺寸(mm)
            particle.diameter_length = fields[2];
            itemText = particle.diameter_length;
            break;
        case 5: // 飞火颗粒加工方法
            particle.processing_method = fields[2];
            itemText = particle.processing_method;
            break;
        case 6: // 燃烧后飞火颗粒质量(g)
            particle.post_burn_weight = fields[2].toDouble();
            itemText = QString::number(particle.post_burn_weight);
            break;
        case 7: // 燃烧后飞火颗粒尺寸(mm)
            particle.post_burn_diameter_length = fields[2];
            itemText = particle.post_burn_diameter_length;
            break;
        case 8: // 飞火颗粒数目（个）
            particle.particle_count = fields[2].toInt();
            itemText = QString::number(particle.particle_count);
            break;
        case 9: // 飞火颗粒来源或产地
            particle.origin = fields[2];
            itemText = particle.origin;
            break;
        case 10: // 飞火颗粒图片
            qDebug() << "飞火颗粒图片相对路径：" << fields[2];
            //itemText = directoryPath + fields[2].replace("..\\" , "").replace("\\", "/");
            qDebug() << "飞火颗粒图片绝对路径：" << itemText;
            particle.image_path = QDir::toNativeSeparators(fields[2]);
            qDebug() << "飞火颗粒图片绝对路径-修正后：" << particle.image_path;
            break;
        default:
            itemText = "";
            break;
        }
    }

    //qDebug() << "\n" << "csv内容: " << "\n" << content;
    file.close();

    return particle;
}

QStringList getSampleInfoHeaders() {
    QStringList headers;
    headers << QObject::tr("试样编号")
            << QObject::tr("试样材料名称")
            << QObject::tr("试样材料类型")
            << QObject::tr("试样质量(g)")
            << QObject::tr("试样尺寸(mm)")
            << QObject::tr("试样厚度(mm)")
            << QObject::tr("试样含水率（%）")
            << QObject::tr("使用年限(无量纲)")
            << QObject::tr("来源或产地(无量纲)")
            << QObject::tr("试样图片");
    return headers;
}

SampleInfo importSampleCsv(const QString &filePath) {
    if (filePath.isEmpty())
    {
        qDebug() << "当前打开的文件路径为" << filePath << "打开失败！";
        // 返回一个默认构造的 SampleInfo 对象
        return SampleInfo();
    }else {
        //显示文件路径
        qDebug() << "当前打开的文件路径为" << filePath;
    }

    // 初始化试样结构体
    SampleInfo sample;

    QFileInfo fileInfo(filePath);
    QString baseName = fileInfo.completeBaseName(); // 获取不包含扩展名的文件名
    qDebug() << "去除扩展名后的文件名为：" << baseName;

    // 读取文件内容
    QFile file(filePath);
    if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
        qDebug() << "无法打开文件：" << filePath;
    }
    QTextStream in(&file);
    // 指定文件编码为GB2312
    in.setCodec(QTextCodec::codecForName("GB2312"));
    QString content;

    QString directoryPath = fileInfo.dir().absolutePath(); // 获取包含文件名的完整路径
    // 去掉文件名，保留目录路径
    directoryPath = directoryPath.left(directoryPath.lastIndexOf("/"));
    // 确保目录路径以正斜杠结束
    if (!directoryPath.endsWith("/")) {
        directoryPath += "/";
    }
    qDebug() << "保留路径为：" << directoryPath;

    while (!in.atEnd()) {
        QString line = in.readLine();
        // 这里可以对每一行数据进行处理
        content.append(line);
        //qDebug() << line << "\n";

        QByteArray byteArray = line.toUtf8();
        QList<QByteArray> fields = byteArray.split(',');
        int column = fields[0].toInt() - 1;
        QString itemText;
        switch (column) {
        case 0: // 试样编号
            // 在读取CSV时已经设置了正确的编码，因此不需要再次转换
            // QString itemText = QString::fromLatin1(fields[2]);
            sample.sample_id = fields[2];
            itemText = sample.sample_id;
            break;
        case 1: // 试样名称
            sample.sample_name = fields[2];
            itemText = sample.sample_name;
            break;
        case 2: // 试样类型
            sample.sample_type = fields[2];
            itemText = sample.sample_type;
            break;
        case 3: // 试样质量(g)
            sample.sample_weight = fields[2].toDouble();
            itemText = QString::number(sample.sample_weight);
            break;
        case 4: // 试样尺寸(mm)
            sample.sample_dimensions = fields[2];
            itemText = sample.sample_dimensions;
            break;
        case 5: // 试样厚度(mm)
            sample.sample_thickness = fields[2].toInt();
            itemText = QString::number(sample.sample_thickness);
            break;
        case 6: // 试样含水率（%）
            sample.sample_water_content = fields[2];
            itemText = sample.sample_water_content;
            break;
        case 7: // 使用年限(无量纲)
            sample.sample_service_life = fields[2];
            itemText = sample.sample_service_life;
            break;
        case 8: // 来源或产地(无量纲)
            sample.sample_origin = fields[2];
            itemText = sample.sample_origin;
            break;
        case 9: // 试样图片
            qDebug() << "试样图片相对路径：" << fields[2];
            //itemText = directoryPath + fields[2].replace("..\\" , "").replace("\\", "/");
            qDebug() << "试样图片绝对路径：" << itemText;
            sample.sample_image_path = QDir::toNativeSeparators(fields[2]);
            qDebug() << "试样图片绝对路径-修正后：" << sample.sample_image_path;
            break;
        default:
            itemText = "";
            break;
        }
    }

    //qDebug() << "\n" << "csv内容: " << "\n" << content;
    file.close();

    return sample;
}

QStringList getExperimentInfoHeaders() {
    QStringList headers;
    headers << QObject::tr("试验编号")
            << QObject::tr("试验时间")
            << QObject::tr("引燃方法")
            << QObject::tr("试样编号")
            << QObject::tr("飞火颗粒编号")
            << QObject::tr("试样材料名称")
            << QObject::tr("飞火颗粒名称")
            << QObject::tr("飞火颗粒数量")
            << QObject::tr("试验风速")
            << QObject::tr("引燃时间")
            << QObject::tr("危险分级")
            << QObject::tr("试验图片");
    return headers;
}

ExperimentInfo importExperimentCsv(const QString &filePath) {
    if (filePath.isEmpty()) {
        qDebug() << "当前打开的文件路径为" << filePath << "打开失败！";
        // 返回一个默认构造的 ExperimentInfo 对象
        return ExperimentInfo();
    } else {
        // 显示文件路径
        qDebug() << "当前打开的文件路径为" << filePath;
    }

    // 初始化试验结构体
    ExperimentInfo experiment;

    QFileInfo fileInfo(filePath);
    QString baseName = fileInfo.completeBaseName(); // 获取不包含扩展名的文件名
    qDebug() << "去除扩展名后的文件名为：" << baseName;

    // 读取文件内容
    QFile file(filePath);
    if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
        qDebug() << "无法打开文件：" << filePath;
    }
    QTextStream in(&file);
    // 指定文件编码为GB2312
    in.setCodec(QTextCodec::codecForName("GB2312"));
    QString content;

    QString directoryPath = fileInfo.dir().absolutePath(); // 获取包含文件名的完整路径
    // 去掉文件名，保留目录路径
    directoryPath = directoryPath.left(directoryPath.lastIndexOf("/"));
    // 确保目录路径以正斜杠结束
    if (!directoryPath.endsWith("/")) {
        directoryPath += "/";
    }
    qDebug() << "保留路径为：" << directoryPath;

    while (!in.atEnd()) {
        QString line = in.readLine();
        // 这里可以对每一行数据进行处理
        content.append(line);
        //qDebug() << line << "\n";

        QByteArray byteArray = line.toUtf8();
        QList<QByteArray> fields = byteArray.split(',');
        int column = fields[0].toInt() - 1;
        QString itemText;
        switch (column) {
        case 0: // 试验编号
            // 在读取CSV时已经设置了正确的编码，因此不需要再次转换
            // QString itemText = QString::fromLatin1(fields[2]);
            experiment.test_id = fields[2];
            itemText = experiment.test_id;
            break;
        case 1: // 试验时间
            experiment.test_time = fields[2];
            itemText = experiment.test_time;
            break;
        case 2: // 引燃方式
            experiment.ignition_method = fields[2];
            itemText = experiment.ignition_method;
            break;
        case 3: // 试样编号
            experiment.sample_id = fields[2];
            itemText = experiment.sample_id;
            break;
        case 4: // 飞火颗粒编号
            experiment.particle_id = fields[2];
            itemText = experiment.particle_id;
            break;
        case 5: // 试样名称
            experiment.sample_name = fields[2];
            itemText = experiment.sample_name;
            break;
        case 6: // 飞火颗粒名称
            experiment.fire_particle = fields[2];
            itemText = experiment.fire_particle;
            break;
        case 7: // 飞火颗粒数量
            experiment.particle_count = fields[2].toInt();
            itemText = QString::number(experiment.particle_count);
            break;
        case 8: // 试验风速
            experiment.test_wind_speed = fields[2];
            itemText = experiment.test_wind_speed;
            break;
        case 9: // 点燃时间
            experiment.ignition_duration = fields[2];
            itemText = experiment.ignition_duration;
            break;
        case 10: // 危险分级
            experiment.hazard_level = fields[2];
            itemText = experiment.hazard_level;
        case 11: // 试验图片路径
            qDebug() << "试验图片相对路径：" << fields[2];
            //itemText = directoryPath + fields[2].replace("..\\" , "").replace("\\", "/");
            qDebug() << "试验图片绝对路径：" << itemText;
            experiment.image_path = QDir::toNativeSeparators(fields[2]);
            qDebug() << "试验图片绝对路径-修正后：" << experiment.image_path;
            break;
        default:
            itemText = "";
            break;
        }
    }

    //qDebug() << "\n" << "csv内容: " << "\n" << content;
    file.close();

    return experiment;
}
